package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import modelInterfaces.ITest;
import utils.Codes;
import utils.Messages;
import controllerInterfaces.IControllerBenchmarkPanel;

/**
 * View to establish the origin of the data of test results and then invoke the function that takes the graph.
 * 
 * @author Andy Cullache
 */
public class DialogSetupTestGraphic extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JFormattedTextField pSplit;
	private IControllerBenchmarkPanel myController;
	private JTextField textField;
	private String nameLoad;
	private String dirPath;
	private JComboBox<ITest> comboBox;
	private ButtonGroup group;
	private JButton okButton;
	private JRadioButton rdbtnTest;
	private JRadioButton rdbtnFile;
	private JButton btnNewButton;


	/**
	 * Principal constructor
	 * 
	 * @param controller      panel controller benchmark.
	 */
	public DialogSetupTestGraphic(IControllerBenchmarkPanel controller) {
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentShown(ComponentEvent arg0) {
				updateTestList();
			}
		});
		this.myController = controller;
		setModal(false);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setTitle("Setup Test Graphic");
		setResizable(false);
		setBounds(100, 100, 479, 174);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		
		pSplit = new JFormattedTextField();
		pSplit.setValue(50);
		pSplit.setColumns(2);
		
		rdbtnFile = new JRadioButton("Result file (.csv)");
		rdbtnFile.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				if(rdbtnFile.isSelected())
					btnNewButton.setEnabled(true);
				else
					btnNewButton.setEnabled(false);
			}
		});
		rdbtnTest = new JRadioButton("Test");
		rdbtnTest.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				if(rdbtnTest.isSelected())
					comboBox.setEnabled(true);
				else
					comboBox.setEnabled(false);
			}
		});
		JLabel lblNewLabel = new JLabel("Generate graphic from :");
		
		btnNewButton = new JButton("Choose");
		btnNewButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				String[] aux = myController.selectDirectory();
				nameLoad = aux[0];
				dirPath = aux[1];
				textField.setText(dirPath);
			}
		});
		
		textField = new JTextField();
		textField.setEditable(false);
		textField.setColumns(10);
		
		comboBox = new JComboBox<ITest>();
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(rdbtnFile)
								.addComponent(rdbtnTest))
							.addGap(18)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPanel.createSequentialGroup()
									.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)
									.addGap(51)
									.addComponent(pSplit, GroupLayout.PREFERRED_SIZE, 0, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_contentPanel.createSequentialGroup()
									.addComponent(btnNewButton)
									.addGap(18)
									.addComponent(textField, GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE))))
						.addComponent(lblNewLabel))
					.addContainerGap())
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(14)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(42)
							.addComponent(pSplit, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblNewLabel)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(rdbtnFile)
								.addComponent(btnNewButton)
								.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(rdbtnTest)
								.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						myController.showGraphicView();
						if(rdbtnTest.isSelected()){
							if(comboBox.getSelectedItem()!=null){
								if(myController.setTestToGraphic((ITest) comboBox.getSelectedItem()))
									dispose();
								else
									renderError(Codes.erroNoTestResult, Messages.erroNoTestResult);
							}
							else{
								renderError(Codes.errorUnselectedTestResult, Messages.errorUnselectedTestResult);
							}
						}
						if(rdbtnFile.isSelected()){
							if(myController.setTestResultToGraphic(dirPath,nameLoad)){
								dispose();
							}
							else{
								renderError(Codes.errorLoadTestResult, Messages.errorLoadTestResult);
							}
									
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		group = new ButtonGroup();
	    group.add(rdbtnFile);
	    group.add(rdbtnTest);
	    rdbtnFile.setSelected(true);
	    comboBox.setEnabled(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}


	/**
	 * Updates the list of Combobox when selected as result origins a test from project.
	 */
	private void updateTestList (){
		List<ITest> tests = myController.getTests();
		for(ITest test : tests){
			comboBox.addItem(test);
		}
	}
	
	/**
	 * Method used to display error messages with the respective code.
	 * 
	 * @param code      Error code.
	 * @param message      Message code.
	 */
	private void renderError(int code, String message) {
		javax.swing.JOptionPane.showMessageDialog(this, code + " - " + message, "Error",javax.swing.JOptionPane.ERROR_MESSAGE);	
	}
}
